<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/12/13
 * Time: 18:20
 */
namespace common\sources\write\shop;


use common\models\craftsman\Shop;
use Yii;

class ShopAccess  implements IShopAccess
{
    public function modifyShop($data)
    {
        $sql = 'UPDATE ' . Shop::tableName() . ' SET is_display = :is_display,update_time=:update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':is_display' => $data['display'],
                ':update_time' => time(),
                ':id' => $data['o_id']
            ])->execute();
    }

    public function PrintExpress($data) {
        $shop = Shop::findOne($data['shopId']);
        if ($shop) {
            $shop->is_print = $data['is_print'];
            if ($shop->save()) {
                return true;
            }
        }
        return false;
    }

    public function AuditBranch($data) {
        $shop = Shop::findOne($data['shopId']);
        if ($shop) {
            $shop->audit_status = $data['audit'];
            $shop->audit_operator = $data['phone'];
            $shop->audit_time = time();
            if ($shop->save()) {
                return true;
            }
        }
        return false;
    }

    public function AddDot($data) {
        $shop = Shop::findOne($data['shopId']);
	    if ($shop) {
		    $shop->dot        = $data['dot'];
		    $shop->shop_name  = $data['shop_name'];
		    $shop->ww_name    = $data['ww_name'];
		    $shop->send_name  = $data['send_name'];
		    $shop->send_phone = $data['send_phone'];
		    $shop->pro_name   = $data['pro_name'];
		    $shop->pro_code   = $data['pro_code'];
		    $shop->city_name  = $data['city_name'];
		    $shop->city_code  = $data['city_code'];
		    $shop->area_name  = $data['area_name'];
		    $shop->addr       = $data['addr'];
		    if ($shop->save()) {
			    return true;
		    }
	    }
        return false;
    }

    public function DelBranch($data) {
        $shop = Shop::find()
            ->where(['id' => $data['shopId'],'audit_status' => 1])
            ->one();
        if ($shop) {
            if ($shop->delete()) {
                return true;
            }
        }
        return false;
    }

    //删除店铺
	public function delShop($id) {
		$sql = 'DELETE FROM `' . Shop::tableName() . '` WHERE  id = :id';
		return Yii::$app->db->createCommand($sql)->bindValues([':id' => $id])->execute();
	}

    public function ModifyBranch($data) {
        $shop = Shop::findOne($data['shopId']);
        if ($shop) {
            $shop->shop_name = $data['shop_name'];
            $shop->ww_name = $data['ww_name'];
            if ($shop->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifyHairInfo($data) {
        $shop = Shop::findOne($data['shopId']);
        if ($shop) {
            $shop->send_name = $data['send_name'];
            $shop->pro_name = $data['pro_name'];
            $shop->pro_code = $data['pro_code'];
            $shop->city_name = $data['city_name'];
            $shop->city_code = $data['city_code'];
            $shop->area_name = $data['area_name'];
            $shop->area_code = $data['area_code'];
            $shop->addr = $data['addr'];
            $shop->dot = $data['dot'];
            $shop->send_phone = $data['send_phone'];
            if ($shop->save()) {
                return true;
            }
        }
        return false;
    }

    public function ModifyBranchType($data) {
        $shop = Shop::findOne($data['shopId']);
        if ($shop) {
            $shop->cate_id = $data['mainCode'];
            $shop->cate_name = $data['mainType'];
            $shop->good_type_id = $data['subCode'];
            $shop->good_type_name = $data['subType'];
            if ($shop->save()) {
                return true;
            }
        }
        return false;
    }
}